package cn.edu.ujn.food.controller;

import cn.edu.ujn.food.entity.*;
import cn.edu.ujn.food.service.*;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@org.springframework.stereotype.Controller
public class RestaurantController {
    @Autowired
    private RestaurantService restaurantService;

    @GetMapping("/restaurant/{id}")
    @ResponseBody
    public ResponseEntity<Restaurant> getById(@PathVariable("id") Integer Id) {
        Restaurant restaurant = restaurantService.queryById(Id);
        if (restaurant == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(restaurant, HttpStatus.OK);
    }

    @PutMapping("/restaurant/{id}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> updateRestaurant(@PathVariable("id") Integer restaurantId, @RequestBody Restaurant restaurant) {
        try {
            restaurant.setRestId(restaurantId); // Assuming there's a setter for restaurantId
            restaurantService.update(restaurant);
            return ResponseEntity.ok(Map.of("success", true, "message", "餐厅信息已成功更新"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/restaurant/{id}")
    public ResponseEntity<Map<String, Object>> deleteRestaurant(@PathVariable("id") Integer restaurantId) {
        try {
            boolean isDeleted = restaurantService.deleteById(restaurantId);
            if (isDeleted) {
                return ResponseEntity.ok(Map.of("success", true, "message", "餐厅信息删除成功！"));
            } else {
                return new ResponseEntity<>(Map.of("success", false, "message", "餐厅信息删除失败，餐厅不存在或已删除。"), HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(Map.of("success", false, "message", "餐厅信息删除失败: " + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/restaurant")
    @ResponseBody
    public String createUser(Restaurant restaurant) {
        try {
            restaurantService.insert(restaurant);
            return "OK";
        } catch (Exception e) {
            return "FAIL";
        }
    }

    @GetMapping("/restaurant")
    public String listRest(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "row",defaultValue = "10") Integer rows, Model model,  Restaurant restaurant) {
        restaurant.setStart((page - 1) * rows);
        restaurant.setRows(rows);
        Page<Restaurant> page1 = restaurantService.selectAll(restaurant);
        page1.setPage(page);
        model.addAttribute("page", page1);
        return "restaurant";
    }
}


